﻿#include "sendsensortvr.h"
#include "ui_sendsensortvr.h"

SendSensorTVR::SendSensorTVR(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SendSensorTVR)
{
    ui->setupUi(this);

    initForm ();
    initCustomplot ();
    initButton ();
}

SendSensorTVR::SendSensorTVR(QVector<double> m1, QVector<double> m2, QWidget *parent):
    QDialog(parent),
    ui(new Ui::SendSensorTVR)
{
      ui->setupUi(this);
      this->data1 = m1;
      this->data2 = m2;
      initForm ();
      initCustomplot ();
      initButton ();
}

SendSensorTVR::~SendSensorTVR()
{
    delete ui;
}

void SendSensorTVR::initForm()
{
    m_totleHeight = 600;                //界面总高度
    m_Width = 700;                     //界面总宽度
    m_titleHeight = 40;                 //标题栏高度

    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    this->setAttribute(Qt::WA_TranslucentBackground,true);
    this->resize(m_Width,m_totleHeight);

    this->setProperty("canMove", QVariant(true));

    QDesktopWidget *desktop = QApplication::desktop();
    this->move((desktop->width()-this->width())/2 , (desktop->height()-this->height())/2);
    this->setWindowOpacity (1);
}

void SendSensorTVR::initCustomplot()
{
    //让坐标系支持拖放，缩放，
    ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->customplot->setBackground(QColor(255, 255, 255));                  //设置背景色
    ui->customplot->addGraph()->setPen (QPen(QColor(50,50,50)));        //设置画笔颜色
    //ui->customplot->xAxis->grid ()->setVisible (false);                 //去除垂直于x轴的默认网格线
    //ui->customplot->yAxis->grid ()->setVisible (false);                 //去除垂直于y轴的默认网格线

    //设置x基准轴颜色
    ui->customplot->xAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->customplot->xAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->customplot->xAxis->setSubTickPen(QColor(41, 113, 120));

    ui->customplot->xAxis->setTickLabelColor(QColor(45, 140, 149));     //设置x轴刻度字体颜色

    ui->customplot->xAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->customplot->xAxis->setLabel("时间");

    //设置y基准轴颜色
    ui->customplot->yAxis->setBasePen(QPen(QColor(45, 140, 149)));
    ui->customplot->yAxis->setTickPen(QPen(QColor(45, 140, 149)));
    ui->customplot->yAxis->setSubTickPen(QColor(41, 113, 120));

    ui->customplot->yAxis->setTickLabelColor(QColor(45, 140, 149));     //设置y轴刻度字体颜色

    ui->customplot->yAxis->setLabelColor(QColor(45, 140, 149));    //设置旁边的标签字体的颜色
    ui->customplot->yAxis->setLabel("幅度");
    QFont yFont = ui->customplot->yAxis->labelFont();               //获取y坐标轴“标签”字体

    //yFont.setPixelSize(20);                                         //设置y轴标签字体大小
     //设置yFont
    ui->customplot->yAxis->setLabelFont(yFont);


    // 设置坐标轴显示范围，否则只能看到默认范围
    ui->customplot->xAxis->setRange(0, 100, Qt::AlignLeft);
   // ui->customplot->xAxis->setRange(-100, 100);
    ui->customplot->yAxis->setRange(0, 1000);

    //ui->customplot->yAxis->setPadding (100);          //设置坐标轴距离边界的距离
    //ui->customplot->xAxis->setOffset (-50);

    ui->customplot->addGraph();
    ui->customplot->graph(0)->setPen(QPen(QColor(179,62,26)));

    //绘图

    auto max1 = std::max_element(std::begin(data1), std::end(data1));

    auto min1 = std::min_element(std::begin(data1), std::end(data1));

    auto max2 = std::max_element(std::begin(data2), std::end(data2));

    auto min2 = std::min_element(std::begin(data2), std::end(data2));


    ui->customplot->xAxis->setRange(*min1, *max1,Qt::AlignLeft);
    ui->customplot->yAxis->setRange(*min2, *max2);

    ui->customplot->graph(0)->setData(data1, data2);
    ui->customplot->replot ();
}

void SendSensorTVR::initButton()
{
    btnClose =new QPushButton(this);
    btnMin = new QPushButton(this);

    //QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    QIcon close_buttonIcon(":/image/btn_close_normal.png");
    QIcon min_buttonIcon(":/image/btn_small_normal.png");

    btnClose->setIcon(close_buttonIcon);
    btnMin->setIcon(min_buttonIcon);
    btnClose->setGeometry(m_Width-40,9,30,30);
    btnMin->setGeometry (m_Width-70,9,30,30);
    btnClose->setStyleSheet("background-color:transparent;");
    btnMin->setStyleSheet("background-color:transparent;");
    //btnSetup->setStyleSheet("color:#7A95B0;background-color:transparent;");

    connect(btnClose,SIGNAL(clicked()),this,SLOT(btnMenu_Close_clicked()));
    connect(btnMin,SIGNAL (clicked()),this,SLOT(btnMenu_Min_clicked()));
}

void SendSensorTVR::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setBrush(QBrush(QColor("#8A8688")));
    painter.setPen(QPen(Qt::transparent));
    painter.drawRect(0,0,m_Width,m_titleHeight);

    painter.setPen(QPen("#000000"));
    painter.setBrush(QBrush(QColor("#FFFFFF")));
    painter.drawRect(0,m_titleHeight,m_Width-1,m_totleHeight-m_titleHeight-1);

    QPixmap pixmap;
    pixmap.load (":/image/FstLogo.ico");
    painter.drawPixmap (10,8,24,24,pixmap);
}

void SendSensorTVR::btnMenu_Close_clicked()
{
    this->close ();
}

void SendSensorTVR::btnMenu_Min_clicked()
{
    this->showMinimized ();
}
